<template>
    <div class="order-management">
        <h2>订单管理</h2>
        <table>
            <thead>
                <tr>
                    <th>订单ID</th>
                    <th>用户ID</th>
                    <th>商品名称</th>
                    <th>数量</th>
                    <th>总价</th>
                    <th>是否支付</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="order in orders" :key="order.id">
                    <td>{{ order.id }}</td>
                    <td>{{ order.userId }}</td>
                    <td>
                        <a href="#" @click.prevent="viewProductDetails(order.product)" class="tooltip">
                            {{ order.product.name }}
                            <span class="tooltiptext">点击查看详情</span>
                        </a>
                    </td>
                    <td>{{ order.quantity }}</td>
                    <td>{{ order.totalPrice | currency }}</td>
                    <td style="text-align: center;min-width: 80px;">
                        <span :class="order.isPay == '1' ? 'paid' : 'unpaid'">
                            {{ order.isPay == '1' ? '已支付' : '未支付' }}
                        </span>
                    </td>
                </tr>
            </tbody>
        </table>

        <!-- 商品详情模态弹窗 -->
        <div v-if="showProductModal" class="modal">
            <div class="modal-content">
                <div class="modal-header">
                    <div></div>
                    <h2>商品详情</h2>
                    <span class="close" @click="closeProductModal">&times;</span>
                </div>
                <div v-if="selectedProduct">
                    <img :src="selectedProduct.imageUrl" alt="商品图片" class="product-image" />
                    <p><strong>名称:</strong> {{ selectedProduct.name }}</p>
                    <p><strong>描述:</strong> {{ selectedProduct.description }}</p>
                    <p><strong>单价:</strong> {{ selectedProduct.price | currency }}</p>
                    <p><strong>库存:</strong> {{ selectedProduct.stock }}</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { get } from "../until/request";

const orders = ref([]);
const showProductModal = ref(false);
const selectedProduct = ref(null);

onMounted(async () => {
    const response = await get('/admin/queryOrderList');
    if (response.code === 1) {
        orders.value = response.data;
    }
});

function viewProductDetails(product) {
    selectedProduct.value = product;
    showProductModal.value = true;
}

function closeProductModal() {
    showProductModal.value = false;
    selectedProduct.value = null;
}
</script>

<style scoped>
.order-management {
    padding: 20px;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th,
td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: left;
}

.paid {
    background-color: #d4edda;
    color: #155724;
    padding: 4px 8px;
    border-radius: 4px;
}

.unpaid {
    background-color: #f8d7da;
    color: #721c24;
    padding: 4px 8px;
    border-radius: 4px;
}

.modal {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 1000;
}

.modal-content {
    background-color: #fff;
    padding: 0 20px 20px 20px;
    border-radius: 8px;
    width: 70%;
    max-height: 100%;
    overflow-y: auto;
    position: relative;
}

.modal-header {
    position: sticky;
    top: 0;
    background-color: #fff;
    z-index: 10;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 10px;
    border-bottom: 1px solid #ddd;
}

.close {
    font-size: 24px;
    cursor: pointer;
}

.product-image {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 20px auto;
}

a {
    color: inherit;
    text-decoration: none;
}

.tooltip {
    position: relative;
    display: inline-block;
    cursor: pointer;
}

.tooltiptext {
    visibility: hidden;
    width: 120px;
    background-color: #555;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    padding: 5px 0;
    position: absolute;
    z-index: 1;
    bottom: 125%;
    /* 调整位置 */
    left: 50%;
    margin-left: -60px;
    opacity: 0;
    transition: opacity 0.3s;
}

.tooltip:hover .tooltiptext {
    visibility: visible;
    opacity: 1;
}
</style>